package com.yonyou.pmclouds.workstopreport.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.billstatus.BillStatusConst;
import com.yonyou.pmclouds.basecom.bp.processor.*;
import com.yonyou.pmclouds.basecom.bp.template.SingleDeleteBpTemplate;
import com.yonyou.pmclouds.basecom.bp.template.SingleInsertBpTemplate;
import com.yonyou.pmclouds.basecom.bp.template.SingleUpdateBpTemplate;
import com.yonyou.pmclouds.basecom.consts.BillTypeConst;
import com.yonyou.pmclouds.basecom.entity.SuperVO;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.project.entity.ProjectVO;
import com.yonyou.pmclouds.project.processor.ProjectBillUpdateForUpdate;
import com.yonyou.pmclouds.project.processor.UpdateBillCountForDeleteProcessor;
import com.yonyou.pmclouds.project.processor.UpdateBillCountForInsertProcessor;
import com.yonyou.pmclouds.project.rmiitf.ProjectSimpleQuery;
import com.yonyou.pmclouds.workflow.aspect.PMApprove;
import com.yonyou.pmclouds.workflow.service.rmiitf.IFlowApproveBuziService;
import com.yonyou.pmclouds.workstopreport.entity.WorkStopVO;
import com.yonyou.pmclouds.workstopreport.mapper.WorkStopMapper;
import com.yonyou.pmclouds.workstopreport.process.*;
import com.yonyou.pmclouds.workstopreport.rmiitf.WorkStopMaintain;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

@Service(version = ApplicationConsts.APPLICATION_VERSION, interfaceClass = com.yonyou.pmclouds.workstopreport.rmiitf.WorkStopMaintain.class )
public class WorkStopMaintainImpl implements WorkStopMaintain, IFlowApproveBuziService {

    @Autowired
    private WorkStopMapper mapper;
    @Autowired
    private ProjectStatusCheckProcessor<WorkStopVO> projectStatusCheckProcessor;
    @Autowired
    private UpdateBillCountForInsertProcessor.UpdateBillCountForInsertProcessorFactory<WorkStopVO> billCountFactory;
    @Autowired
    private UpdateBillCountForDeleteProcessor.UpdateBillCountForDeleteProcessorFactory<WorkStopVO> billCountDeleteFactory;
    @Autowired
    private ProjectBillUpdateForUpdate.ProjectBillForUpdateProcessorFactory<WorkStopVO> billForUpdateProcessorFactory;
    @Autowired
    private WorkStopProjectLogPostProcess workStopProjectLogPostProcess;
    @Autowired
    private WorkStopCopyPersonUpdateBatchProcessor workStopCopyPersonUpdateBatchProcessor;
    @Autowired
    private WorkStopWriteBackProjectProcessor workStopWriteBackProjectProcessor;
    @Autowired
    private WorkStopRepeatCommitProcessor workStopRepeatCommitProcessor;
    @Autowired
    private DocCenterProcessor docCenterProcessor;
    @Autowired
    private WorkStopNCCProcessor workStopNCCProcessor;
    @Autowired
    private WorkStopInsertCheckProcessor workStopInsertCheckProcessor;
    @Autowired
    private WorkStopSaveCopyPersonProcess workStopCopyPersonProcess;
    @Autowired
    private WorkStopPicAndVideoProcess workStopPicAndVideoProcess;

    @Autowired
    private ProjectSimpleQuery projectSimpleQuery;

    @Autowired
    private WorkStopLocalProjectProcessor workStopLocalProjectProcessor;

    //更新状态字段
    private static String[] UPDATE_STATUS = {WorkStopVO.BILLSTATUS};

    //主表需要更新的字段名
    private static String[] UPDATE_FIELDS = {WorkStopVO.STOPDATE,WorkStopVO.PRERESUMEDATE,WorkStopVO.WORKSTOPDAYS};

    @PMApprove
    @Override
    public WorkStopVO insert(WorkStopVO workStopVO) throws BusinessException {
        //创建一个单实体得插入模板对象
        SingleInsertBpTemplate<WorkStopVO> bp = new SingleInsertBpTemplate<>(mapper);
        //补全字段创建者以及创建时间
        bp.addBeforeProcessor(new AuditInfoInsertProcessor<WorkStopVO>());
        //判断是否为空
        bp.addBeforeProcessor(workStopInsertCheckProcessor);
        //已经停工的项目无法重复停工
        bp.addBeforeProcessor(workStopRepeatCommitProcessor);
        //结束的项目无法添加
        bp.addBeforeProcessor(projectStatusCheckProcessor);
        //更新单据数量统计
        bp.addAfterProcessor(billCountFactory.getProcessor(BillTypeConst.WORKSTOP_BILLTYPE));
        //智能日志
        bp.addAfterProcessor(workStopProjectLogPostProcess);
        //保存抄送人
        bp.addAfterProcessor(workStopCopyPersonProcess);
        //存储图片和小视频
        bp.addAfterProcessor(workStopPicAndVideoProcess);
        //添加单据，统一调用process方法
        bp.insert(workStopVO);
        return workStopVO;
    }

    @PMApprove(trigger = "commit")
    @Override
    public WorkStopVO update(WorkStopVO workStopVO) throws BusinessException {
        //创建一个单个实体更新模板对象，传入你要更新的实体的mapper对象以及你想要更新或者说可以更新的字段名。这样在更新时会自动只更新这些字段。
        SingleUpdateBpTemplate<WorkStopVO> bp = new SingleUpdateBpTemplate<>(mapper, UPDATE_FIELDS);
        //判断是否为空
        bp.addBeforeProcessor(workStopInsertCheckProcessor);
        //添加修改者以及修改时间
        bp.addBeforeProcessor(new AuditInfoUpdateProcessor<WorkStopVO>());
        //检验项目状态，判断项目是否已经竣工
        bp.addBeforeProcessor(projectStatusCheckProcessor);
        //更新统计单据，主要更新项目单据统计单据状态
        bp.addAfterProcessor(billForUpdateProcessorFactory.getProcessor(BillTypeConst.WORKSTOP_BILLTYPE));
        //保存抄送人
        bp.addAfterProcessor(workStopCopyPersonUpdateBatchProcessor);
        //存储图片和小视频
        bp.addAfterProcessor(workStopPicAndVideoProcess);
        //更新主表数据
        bp.update(workStopVO);
        return workStopVO;
    }

    @Override
    public WorkStopVO approveByPkBill(String pk_bill, byte billStatus) throws BusinessException {
        if (StringUtils.isEmpty(pk_bill)) {
            return null;
        }
        WorkStopVO workStopVO = mapper.getById(pk_bill);
        SingleUpdateBpTemplate<WorkStopVO> bp = new SingleUpdateBpTemplate<>(mapper, UPDATE_STATUS);

        // 回写预计复工日期(和停工日期,为计算项目停工天数准备，业务上没有要求)
        bp.addAfterProcessor(workStopWriteBackProjectProcessor);

        ProjectVO projectVO = projectSimpleQuery.queryProject(workStopVO.getPkProject());

        if (projectVO == null ||projectVO.getSrcType() == null ||"~".equals(projectVO.getSrcType())) {
            //本地项目状态处理
            bp.addAfterProcessor(workStopLocalProjectProcessor);
        }else {
            //ncc驱动项目状态
            bp.addAfterProcessor(workStopNCCProcessor);
        }

        bp.addAfterProcessor(docCenterProcessor);


        bp.update(workStopVO);

        return workStopVO;
    }

    @Override
    public WorkStopVO rejectByPkBill(String pk_bill) throws BusinessException {
        if(StringUtils.isEmpty(pk_bill)){
            return null;
        }
        //根据单据主键获得停工单实体
        WorkStopVO workStopVO = mapper.selectReport(pk_bill);
        //告诉更改模板要更改的字段
        SingleUpdateBpTemplate<WorkStopVO> bp = new SingleUpdateBpTemplate<>(mapper, UPDATE_STATUS);
        //将实体的状态改变
        workStopVO.setBillstatus(BillStatusConst.COMMON_REJECT_STATUS);
        //更新统计单据，主要更新项目单据统计单据状态
        bp.addAfterProcessor(billForUpdateProcessorFactory.getProcessor(BillTypeConst.WORKSTOP_BILLTYPE));
        bp.update(workStopVO);
        return workStopVO;
    }

    @Override
    public SuperVO deleteBill(SuperVO businessVO) throws BusinessException {
        SingleDeleteBpTemplate<WorkStopVO> bp = new SingleDeleteBpTemplate<>(mapper);
        bp.addAfterProcessor(billCountDeleteFactory.getProcessor(BillTypeConst.WORKSTOP_BILLTYPE));
        return bp.delete((WorkStopVO) businessVO);
    }

    @Override
    public void delete(WorkStopVO workStopVO) throws BusinessException {
        SingleDeleteBpTemplate<WorkStopVO> bp = new SingleDeleteBpTemplate<>(mapper);
        //更新单据
        bp.addAfterProcessor(billCountDeleteFactory.getProcessor(BillTypeConst.WORKSTOP_BILLTYPE));
        bp.delete(workStopVO);
    }

    @Override
    public SuperVO queryDetailForWorkFlow(String pk_bill) {
        return mapper.selectReport(pk_bill);
    }

    @Override
    public Object getMapper() {
        return mapper;
    }

    @Override
    public String getBillType() {
        return BillTypeConst.WORKSTOP_BILLTYPE;
    }
}
